package cn.sx.controller;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc:  后台用户登录控制器
 *@Time: 2020/9/18  9:57
 */

import cn.sx.entity.*;
import cn.sx.service.*;
import cn.sx.util.PageUtil;
import cn.sx.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Types;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private TypesService typesService;
    @Autowired
    private TopService topService;
    @Autowired
    private OrdersService ordersService;

    //后台登录相关

    /*跳转后台登录界面 */
    @RequestMapping("/login")
    public  String goAdmin(){
        return "/admin/login.jsp";
    }

    /*登录*/
    @PostMapping("/login")
    public String AdminLogin(Admins admins, HttpSession session, HttpServletRequest request){
        Admins adminsLogin=adminService.getByUsernameAndPassword(admins.getUsername(),admins.getPassword());
        if(adminsLogin ==null){
            request.setAttribute("msg","用户名或密码错误");
            return "/admin/login.jsp";
        }else {
            session.setAttribute("admins",adminsLogin);
            return "redirect:index";
        }
    }
    /*退出登录*/
    @RequestMapping("/logout")
    public String doLogout(HttpSession sess){
        try {
            sess.removeAttribute("admin");
        }catch (Exception e){
        }
        try {
            sess.removeAttribute("msg");

        }catch (Exception e){
        }
        return "redirect:/index/index";
    }
    /*后台首页*/
    @RequestMapping("/index")
    public String goAdminIndex(HttpServletRequest request){
        request.setAttribute("msg","欢迎进入后台管理界面！大哥请进！===>");
        return "/admin/index.jsp";
    }




    //后台类目管理相关

    /*跳转类目管理界面*/
    @GetMapping("/typeList")
    public String findAallTypes(Model model,HttpServletRequest request,
                                @RequestParam(required = false,defaultValue = "1")int page,
                                @RequestParam(required = false,defaultValue = "2")int size){
        List<Type> list = typesService.findAll();
        request.setAttribute("flag",1);
        model.addAttribute("typeList",list);
        return "/admin/type_list.jsp";
    }
    /*跳转类目管理修改界面*/
    @GetMapping("/typeEdit")
    public String typeEdit(int id,HttpServletRequest request){
        request.setAttribute("flag",1);
        request.setAttribute("type",typesService.get(id));
        return "/admin/type_edit.jsp";
    }
    /*根据id修改后台管理类目*/
    @PostMapping("/typeUpdate")
    public String updateType(int id,HttpSession session,String  name,int num ,HttpServletRequest request){
        Type type = typesService.get(id);
        request.setAttribute("flag",1);
        typesService.update(type.getId(),name,num);
        session.setAttribute("type",typesService.get(type.getId()));
        request.setAttribute("msg","修改成功！");
        return "/admin/type_edit.jsp";
    }
    /*后台类目管理删除*/
    @RequestMapping("/typeDelete")
    public String deleteType(int id){
        typesService.deleteType(id);
        return "redirect:typeList";
    }
    /*跳转类目管理添加界面*/
    @RequestMapping("/typeAdd")
    public String goTypeAdd(){
        return "/admin/type_add.jsp";
    }
    /*后台管理添加类目管理*/
    @PostMapping("/typeSave")
    public String typeAdd(Type types, HttpServletRequest request){
        if (types.getName().isEmpty()){
            request.setAttribute("msg","类目名不能为空！");
        }else if (Objects.nonNull(typesService.selectByTypeName(types.getName()))){
            request.setAttribute("msg","用户已经存在！");
        }else {
            typesService.typeAdd(types);
            request.setAttribute("msg","添加成功");
            return "/admin/type_add.jsp";
        }
        return "/admin/type_add.jsp";
    }




    //后台商品管理相关


    /*跳转商品管理界面*/
    @RequestMapping("/goodList")
    public String goGoodList(Model model,HttpServletRequest request,
                             @RequestParam(required = false,defaultValue = "0")byte type,
                             @RequestParam(required = false,defaultValue = "1")int page,
                             @RequestParam(required = false,defaultValue = "10")int size){
        request.setAttribute("flag",2);
        request.setAttribute("type",type);
        request.setAttribute("page",page);
        if (type >0) {//去今日推荐
            String goodIds=topService.getGoodIdsByType(type);
            request.setAttribute("goodList",goodsService.getListByIds(goodIds,page,size));
            request.setAttribute("pageTool",PageUtil.getPageTool(request,goodsService.getCountByIds(goodIds),page,size));
        }else {//去全部商品
            List<Goods> list = goodsService.selectAllGoods(page, size);
            model.addAttribute("goodList",list);
            request.setAttribute("pageTool", PageUtil.getPageTool(request,goodsService.getCount(),page,size));
        }
        return "/admin/good_list.jsp";
    }

    /*跳转商品商品管理添加界面*/
    @RequestMapping("/goodAdd")
    public String goGoodAdd(HttpServletRequest request){
        request.setAttribute("flag",2);
        request.setAttribute("typeList",typesService.findAll());
        return "/admin/good_add.jsp";
    }
    /*添加商品*/
    @RequestMapping("/goodSave")
    public String goodSave(Goods goods , MultipartFile file,
                           @RequestParam(required = false,defaultValue = "1")int page)throws Exception{
        goods.setCover(UploadUtil.upload(file));
        goodsService.add(goods);
        return  "redirect:goodList?flag=2&page="+page;
    }
    /*修改商品信息*/
    @GetMapping("/goodEdit")
    public String goUpdateGood(int id,HttpServletRequest request){
        request.setAttribute("flag",2);
        request.setAttribute("typeList",typesService.findAll());
        request.setAttribute("good",goodsService.get(id));
        return "/admin/good_edit.jsp";
    }
    @PostMapping("/goodUpdate")
    public String goodUpdate(Goods goods,MultipartFile file,
                             @RequestParam(required = false,defaultValue = "1")int page)throws Exception{
        if (Objects.nonNull(file) && !file.isEmpty()){
            goods.setCover(UploadUtil.upload(file));
        }
        goodsService.update(goods);
        return "redirect:goodList?flag=2&page="+page;
    }
    /*删除商品*/
    @GetMapping("/goodDelete")
    public String goodDelete(int id,
                             @RequestParam(required = false,defaultValue = "1")int page){
        goodsService.delete(id);
        return "redirect:goodList?flag=2&page="+page;
    }

    /*添加今日推荐*/
    @PostMapping("/topSave")
    public @ResponseBody String topSave(int goodId,byte type){
        return topService.add(goodId,type) ? "ok" : null;
    }
    /*移除今日推荐*/
    @PostMapping("/topDelete")
    public @ResponseBody String topDelete(int goodId,byte type){
        return topService.delete(goodId,type) ? "ok" : null;
    }





    //后台订单相关

    /*跳转订单管理界面*/
    @RequestMapping("/orderList")
    public String goOrderList(@RequestParam(required = false,defaultValue = "1")int page,
                              @RequestParam(required = false,defaultValue = "10")int siza,
                              @RequestParam(required = false,defaultValue = "0")byte status,
                              HttpServletRequest request){
        request.setAttribute("flag",3);
        request.setAttribute("status",status);
        request.setAttribute("page",page);
        request.setAttribute("orderList",ordersService.getList(status,page,siza));
        request.setAttribute("pageTool",PageUtil.getPageTool(request,ordersService.getCount(status),page,siza));
        return "/admin/order_list.jsp";
    }

    /*订单发货*/
    @GetMapping("/orderSend")
    public String orderSend(int id,byte status,@RequestParam(required = false,defaultValue = "1")int page){
        ordersService.send(id);
        return "redirect:orderList?flag=3&status="+status+"&page="+page;

    }

    /*订单完成*/
    @GetMapping("/orderFinish")
    public String orderFinsih(int id,byte status,@RequestParam(required = false,defaultValue = "1")int page){
        ordersService.finsh(id);
        return "redirect:orderList?flag=3&status="+status+"&page="+page;
    }

    /*删除订单*/
    @GetMapping("/orderDelete")
    public String orderDelete(int id,byte status,@RequestParam(required = false,defaultValue = "1")int page){
        ordersService.delete(id);
        return "redirect:orderList?flag=3&status="+status+"&page="+page;

    }






    //后台用户管理相关

    /*跳转用户管理界面*/
    @RequestMapping("/userList")
    public String goUserList(Model model,HttpServletRequest request,
                             @RequestParam(required = false,defaultValue = "1")int page,
                             @RequestParam(required = false,defaultValue = "6")int size){
        List<User> list = userService.findAllUser();
        request.setAttribute("flag",4);
        model.addAttribute("userList",list);
        request.setAttribute("pageTool",PageUtil.getPageTool(request,userService.getCount(),page,size));
        return "/admin/user_list.jsp";
    }
    /*跳转后台用户添加界面*/
    @RequestMapping("/userAdd")
    public String goUserAdd(){
        return "/admin/user_add.jsp";
    }
    /*后台用户管理添加用户*/
    @PostMapping("/userSave")
    public String addUser(User user , HttpServletRequest request){
        if (user.getUsername().isEmpty()) {
            request.setAttribute("msg","用户名不能为空！");
        }else if(Objects.nonNull(userService.getByUsername(user.getUsername()))){
            request.setAttribute("msg","用户已经存在！");
        }else{
            userService.add(user);
            request.setAttribute("msg","添加成功");
            return "/admin/user_add.jsp";
        }
        return "/admin/user_add.jsp";
    }
    /*跳转后台用户重置密码界面*/
    @GetMapping("/userRe")
    public String goUserRe(int id ,HttpServletRequest request){
        request.setAttribute("user",userService.get(id));
        return "/admin/user_reset.jsp";
    }
    /*修改后台用户密码*/
    @PostMapping("/userReset")
    public String updateUserPassword(int id,String password,HttpServletRequest request){
        User user = userService.get(id);
        userService.updateUsersPassword(user.getId(),password);
        request.setAttribute("flag",4);
        request.setAttribute("user",userService.get(user.getId()));
        request.setAttribute("msg","修改成功");
        return "/admin/user_reset.jsp";
    }
    /*跳转后台用户管理修改界面*/
    @GetMapping("/userEdit")
    public String goUserEdit(int id,HttpServletRequest request){
//        request.setAttribute("flag",4);
        request.setAttribute("user",userService.get(id));
        return "/admin/user_edit.jsp";
    }
    /*修改后台用户信息*/
    @PostMapping("/userUpdate")
    public String updateUser(int id,String name ,String phone,String address,HttpServletRequest request,HttpSession session){
        User user = userService.get(id);
        adminService.updateUser(user.getId(),name,phone,address);
        session.setAttribute("user",userService.get(user.getId()));
        request.setAttribute("msg","修改成功");
        return "/admin/user_edit.jsp";
    }
    /*后台用户管理删除*/
    @RequestMapping("/userDelete")
    public String userDelete(int id){
        userService.deleteUser(id);
        return "redirect:userList";
    }





    //后台管理员相关

    /*跳转管理员界面*/
    @RequestMapping("/adminList")
    public String goAdminList(Model model,HttpServletRequest request,
                              @RequestParam(required = false,defaultValue = "1")int page,
                              @RequestParam(required = false,defaultValue = "6")int size){
        List<Admins> list = adminService.findAllAdmins();
        model.addAttribute("adminList",list);
        request.setAttribute("pageTool",PageUtil.getPageTool(request,adminService.getCount(),page,size));
        return "/admin/admin_list.jsp";
    }
    /*跳转添加管理员界面*/
    @RequestMapping("/adminAdd")
    public String goAdminAdd(){
        return "/admin/admin_add.jsp";
    }
    /*添加后台管理员*/
    @PostMapping("/adminSave")
    public String addAdmin(Admins admins,HttpServletRequest request){
        if(admins.getUsername().isEmpty()){
            request.setAttribute("msg","管理员名称不能为空");
        }else if(Objects.nonNull(adminService.selectAdminByUserame(admins.getUsername()))){
            request.setAttribute("msg","管理员已经存在");
        }else {
            adminService.addAdmin(admins);
            request.setAttribute("msg","添加成功");
            return "/admin/admin_add.jsp";
        }
        return "/admin/admin_add.jsp";
    }
    /*跳转管理员重置密码界面*/
    @GetMapping("/adminRe")
    public String goAdminRe(int id,HttpServletRequest request,HttpSession session){
        request.setAttribute("flag",5);
        request.setAttribute("admin",adminService.get(id));
        return "/admin/admin_reset.jsp";
    }
    /*后台重置密码*/
    @PostMapping("/adminReset")
    public String adminReset(int id,String password,HttpServletRequest request){
        Admins admins = adminService.get(id);
        adminService.updatePassword(admins.getId(),password);
        request.setAttribute("flag",5);
        request.setAttribute("admin",adminService.get(admins.getId()));
        request.setAttribute("msg","修改成功");
        return "/admin/admin_reset.jsp";
    }

    /*跳转管理员删除界面*/
    @GetMapping("/adminDelete")
    public String deleteAdmin(int id){
        adminService.deleteAdmin(id);
        return "redirect:adminList";
    }
}
